package xyz.maxwells.bigdata.ctrl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.maxwells.bigdata.domain.RequestResult;
import xyz.maxwells.bigdata.domain.SysUser;
import xyz.maxwells.bigdata.domain.VuePageable;
import xyz.maxwells.bigdata.domain.data.Comment;
import xyz.maxwells.bigdata.domain.data.CommentFindAllDTO;
import xyz.maxwells.bigdata.domain.data.FindCommentTable;
import xyz.maxwells.bigdata.domain.data.UpdategoodsOrBads;
import xyz.maxwells.bigdata.exception.BigdataException;
import xyz.maxwells.bigdata.service.CommentService;

import java.util.Date;

/**
 * Created by wuzusheng on 2018/5/13.
 */
@RestController
public class CommentCtrl {
    @Autowired
    private CommentService service;
    @RequestMapping("/savecomment")
    public RequestResult saveComment(String comments,String tableName) throws BigdataException{
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String){
            throw new BigdataException("bigdata_01_006::尚未登录,无法获取当前用户");
        }
        SysUser securityUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Comment comment = new Comment();
        comment.setUserName(securityUser.getUsername());
        comment.setData(new Date());
        comment.setComments(comments);
        comment.setTableName(tableName);
        service.save(comment);
        return new RequestResult();
    }
    @RequestMapping("/deletecomment")
    public RequestResult delete(Long id){
        service.delete(id);
        return new RequestResult();
    }
    @RequestMapping("/updategoods")
    public RequestResult update(UpdategoodsOrBads updategoodsOrBads){
        Comment comment = service.findone(updategoodsOrBads.getId());
        if (updategoodsOrBads.getBads()!=null){
            comment.setBads(comment.getBads()+updategoodsOrBads.getBads());
        }
        if (updategoodsOrBads.getGoods()!=null){
            comment.setGoods(comment.getGoods()+updategoodsOrBads.getGoods());
        }
        service.save(comment);
        return new RequestResult();
    }
    @RequestMapping("/findcomment")
    public RequestResult findComments(FindCommentTable findCommentTable){
        if (findCommentTable.getHot()!=null){
            return new RequestResult(service.findByTableName(findCommentTable.getTableName(),new Sort(Sort.Direction.DESC,"goods")));
        }
        if (findCommentTable.getLast()!=null){
            return new RequestResult(service.findByTableName(findCommentTable.getTableName(),new Sort(Sort.Direction.ASC,"data")));
        }

        return new RequestResult(service.findByTableName(findCommentTable.getTableName(),new Sort(Sort.Direction.DESC,"data")));
    }
    @RequestMapping("/listcomment")
    public RequestResult listcommnet(CommentFindAllDTO dto,VuePageable pageable){
        Page<Comment> page = service.list(CommentFindAllDTO.getSpecification(dto),pageable.getPageable());
        return new RequestResult(page);
    }
    @RequestMapping("/updatecomment")
    public RequestResult update(Long id,String tableName){
        Comment comment = service.findone(id);
        comment.setTableName(tableName);
        service.save(comment);
        return new RequestResult();
    }
    @RequestMapping("/updatereportcomment")
    public RequestResult updatereport(Long id,String reportReason){
        Comment comment = service.findone(id);
        if (reportReason == null || reportReason == ""){
            comment.setReportReason(null);
        }else {
            comment.setReportReason(reportReason);
        }
        service.save(comment);
        return new RequestResult();
    }
    @RequestMapping("/report")
    public RequestResult report(Long id,String reportReason)throws BigdataException{
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String){
            throw new BigdataException("bigdata_01_006::尚未登录,无法获取当前用户");
        }
        SysUser securityUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Comment comment = service.findone(id);

        comment.setReportData(new Date());
        if (comment.getReportReason()==null){
            comment.setReportReason(reportReason);
        }else {
            comment.setReportReason(comment.getReportReason()+" | "+reportReason);
        }
        if (comment.getReporter()==null){
            comment.setReporter(securityUser.getUsername());
        }else {
            comment.setReporter(securityUser.getUsername()+" | "+securityUser.getUsername());
        }

        service.save(comment);
        return new RequestResult();
    }
}
